<template>
  <view class="pay">
    <!-- 收货地址 -->
    <view class="address u-p-20">
      <!-- 按钮选择地址 -->
      <view class="address-btn u-flex u-row-center" v-if="!address">
        <u-button
          shape="circle"
          type="error"
          plain
          size="medium"
          :custom-style="{ backgroundColor: '#fff !important' }"
          @click="handleAddress"
          >选择地址</u-button
        >
      </view>
      <!-- 数据渲染 -->
      <view class="info" v-else>
        <view class="address-site">{{ addressInfo }}</view>
        <view class="address-user"
          >{{ address.userName }} {{ address.telNumber }}</view
        >
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 已选商品 -->
    <view class="goods-list u-p-15">
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view
          class="cart-item"
          v-for="item in checkedCartList"
          :key="item.goods_id"
        >
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              width="191rpx"
              height="191rpx"
              :src="item.goods_small_logo"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">X{{ item.nums }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price">
        合计: <text>￥{{ checkedPrice }}</text>
      </view>
      <view class="all-count">
        <u-button @click="handlePay" type="error" shape="circle"
          >去支付({{ checkedCount }})</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      address: null,
    };
  },

  computed: {
    ...mapState("user", ["token"]),
    ...mapGetters("cart", ["checkedCartList", "checkedPrice", "checkedCount"]),
    addressInfo() {
      if (!this.address) return "";
      return (
        this.address.provinceName +
        this.address.cityName +
        this.address.countyName +
        this.address.detailInfo
      );
    },
  },
  methods: {
    ...mapActions("user", ["fetchToken"]),
    async handleAddress() {
      const [err, res] = await uni.chooseAddress();
      if (!err) {
        this.address = res;
      }
    },
    // 点击去支付
    async handlePay() {
      if (!this.address) {
        uni.showToast({
          title: "您还没有选择收货地址",
          icon: "none",
        });
        return;
      }

      //  判断当前有没有token 有 就直接使用 没有 获取 即可！！
      // 获取token
      if (!this.token) {
        await this.fetchToken();
      }

      //  准备创建订单要的参数
      const orderParams = {
        order_price: this.checkedPrice,
        consignee_addr: `${this.addressInfo}|${this.address.userName}|${this.address.telNumber}`,
        goods: this.checkedCartList.map((item) => ({
          goods_id: item.goods_id,
          goods_number: item.nums,
          goods_price: item.goods_price,
        })),
      };

      //  发送请求 获取 订单编号 axios 传参的写法 我不确定！！
      const { order_number } = await this.$u.post(
        "/my/orders/create",
        orderParams
      );

      //  创建支付流程 获取支付参数
      const { pay } = await this.$u.post("/my/orders/req_unifiedorder", {
        order_number,
      });

      // 调起微信支付
      const res7 = await uni.requestPayment(pay);
    },
  },
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;

  .cart-item {
    display: flex;
    padding: 10rpx;
    border-bottom: 1rpx solid #ccc;

    .goods-img {
      flex: 2;

      display: flex;
      align-items: center;
      justify-content: center;
    }

    .goods-info {
      color: #666;
      flex: 3;
      display: flex;
      flex-direction: column;
      justify-content: space-around;

      .goods-price-num {
        display: flex;
        justify-content: space-between;
        .goods-price {
          color: $themeColor;
        }

        .goods-num-tool {
          // width: 140rpx;
          height: 40rpx;
          display: flex;
          justify-content: space-between;
        }
      }
    }
  }
  .statistics {
    position: fixed;
    background-color: #fff;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    border-top: 1rpx solid #ccc;
    display: flex;
    align-items: center;
    padding: 15rpx;
    .all-chk {
    }

    .all-price {
      flex: 1;
      color: #666;
      text {
        color: $themeColor;
      }
    }

    .all-count {
    }
  }
}
</style>